home *** CD-ROM | disk | FTP | other *** search
/ The PC-SIG Library 9 / The PC-SIG Library on CD ROM - Ninth Edition.iso / 1501_600 / DISK1533 / DISK1533.ZIP / DLT.C < prev    next >
Text File  |  1989-02-22  |  2KB  |  92 lines

  1. #include <stdio.h>
  2. #include <stat.h>
  3.  
  4. /* DLT.C - delete multiple files on the command line, the DOS DEL and ERASE */
  5. /* commands cannot delete more than 1 file without the wildcard masks used  */
  6.  
  7. static    char *Program[] = { "DLT - replmnt delete routine"
  8.                 "S. Leoce - V1 R1 S0 - VA/PSJ" };
  9.  
  10. static    char *Usage = "usage: dlt [/cft] file [file ...]";
  11.  
  12. #include "b:cmdline.c"
  13.  
  14. #define    FALSE    0
  15. #define    TRUE    ~FALSE
  16.  
  17. int main (argc, argv)
  18. int argc;
  19. char *argv[];
  20. {
  21.     short unsigned int LineOption = FALSE;
  22.     FILE    *CurF;
  23.     int    CurFH;    /* current file, and handle here */
  24.  
  25.     long    unsigned register int    files = 0;    /* files counter */
  26.     auto    short     int        type  =    FALSE;    /* type names      */
  27.     auto    short     int        count =    FALSE;    /* count files     */
  28.     auto    short     int        force = FALSE;
  29.  
  30.     if (argc < 2)
  31.         _exit (fprintf(stderr, "%s\n", Usage) + 0x10);
  32.     
  33.     opterr = FALSE;
  34.     while ((LineOption = getopt(argc, argv, "cft")) != EOF)
  35.         switch(LineOption) {
  36.             case '?':
  37.                 _exit(fprintf(stderr, "%s\n", Usage) + 0x10);
  38.             case 'c':
  39.                 count = TRUE;
  40.                 break;
  41.             case 'f':
  42.                 force = TRUE;
  43.                 break;
  44.             case 't':
  45.                 type = TRUE;
  46.                 break;
  47.         }
  48.     if (argv[optind] == NULL)
  49.         _exit(fprintf(stderr, "%s\n", Usage) + 0x12);
  50.  
  51.     if (!force) {
  52.         while (argv[optind] != NULL) {
  53.             if(unlink(argv[optind]) != 0) {
  54.                 if(type) {
  55.                     fprintf(stderr,"%s ",argv[optind]);
  56.                     fprintf(stderr,"force may be required\n");
  57.                 }
  58.                 else
  59.                     perror(argv[optind]);
  60.             }
  61.             else {
  62.                 if (type)
  63.                     fprintf(stdout,"%s removed\n", argv[optind]);
  64.                 files++;
  65.             }
  66.                         
  67.             optind++;
  68.         }
  69.  
  70.         if(count)
  71.             fprintf(stdout,"\n%10ld files removed\n", files);
  72.  
  73.         return (0);
  74.     }
  75.     else     /* chmod all files first, then remove     */
  76.         while(argv[optind] != NULL) {
  77.             if(chmod(argv[optind], S_IREAD|S_IWRITE))
  78.                 fprintf(stderr,"%s couldn't force\n",argv[optind]);
  79.             else if(unlink(argv[optind]) != 0)
  80.                 perror(argv[optind]);
  81.             else {
  82.                 if (type)
  83.                     fprintf(stdout,"%s removed\n",argv[optind]);
  84.                 files++;
  85.             }
  86.             optind++;
  87.         }
  88.     if (count)
  89.         fprintf(stdout,"\n%10ld files removed\n", files);
  90.  
  91.     return (0);
  92. }